package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowProductService;
import com.qidianit.mes.sys.mapper.MaterialMapper;
import com.qidianit.mes.sys.mapper.UnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/18 0018 - 19:29
 */
@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {
    @Autowired
    private WorkflowProductMapper workflowProductMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Override
    public Result<PageInfo<WorkflowProduct>> selectList(Long workflowId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<WorkflowProduct> workflowProducts = workflowProductMapper.selectList(workflowId);
        for (WorkflowProduct workflowProduct : workflowProducts) {
            Material material = materialMapper.selectByCodeMaterial(workflowProduct.getProductCode());
            workflowProduct.setProduct(material);
            material.setUnit(unitMapper.selectByIdUnit(material.getUnitId()));
        }
        return Result.success(new PageInfo<>(workflowProducts));
    }

    @Override
    public Result insertWorkflowProduct(WorkflowProduct workflowProduct) {
        if (workflowProduct.getProductCode() == null || workflowProduct.getProductCode().trim().isEmpty() ||
                workflowProduct.getRemarks() == null || workflowProduct.getRemarks().trim().isEmpty() ||
                workflowProduct.getWorkflowId() == null){
            return Result.error("操作失败");
        }
        ArrayList<WorkflowProduct> workflowProducts = workflowProductMapper.selectList(workflowProduct.getWorkflowId());
        for (WorkflowProduct product : workflowProducts) {
            if (Objects.equals(product.getProductCode(), workflowProduct.getProductCode())){
                return Result.error("该产品/物料已经添加");
            }
        }
        return Result.success(workflowProductMapper.insertWorkflowProduct(workflowProduct));
    }

    @Override
    public Result<WorkflowProduct> selectById(Long id) {
        WorkflowProduct workflowProduct = workflowProductMapper.selectById(id);
        Material material = materialMapper.selectByCodeMaterial(workflowProduct.getProductCode());
        workflowProduct.setProduct(material);
        material.setUnit(unitMapper.selectByIdUnit(material.getId()));
        return Result.success(workflowProduct);
    }

    @Override
    public Result updateById(WorkflowProduct workflowProduct) {
        if (workflowProduct.getProductCode() == null || workflowProduct.getProductCode().trim().isEmpty() ||
                workflowProduct.getRemarks() == null || workflowProduct.getRemarks().trim().isEmpty() ||
                workflowProduct.getWorkflowId() == null){
            return Result.error("操作失败");
        }
        ArrayList<WorkflowProduct> workflowProducts = workflowProductMapper.selectList(workflowProduct.getWorkflowId());
        for (WorkflowProduct product : workflowProducts) {
            if (Objects.equals(product.getProductCode(), workflowProduct.getProductCode())
                    && !Objects.equals(product.getId(), workflowProduct.getId())){
                return Result.error("该产品/物料已经添加");
            }
        }
        return Result.success(workflowProductMapper.updateById(workflowProduct));
    }

    @Override
    public Result deleteById(Long id) {
        return Result.success(workflowProductMapper.deleteById(id));
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProductMapper.batchDelete(ids) > 0?Result.success():Result.error("操作失败");

    }


}